package com.ewuyong.concurrence.example.aqs;

import com.ewuyong.concurrence.annotations.ThreadSafe;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

@Slf4j
public class CountDownLatchExample {

    public static int threadTotal = 200;

    public static void main(String[] args) throws InterruptedException {

        CountDownLatch countDownLatch = new CountDownLatch(threadTotal);

        ExecutorService executorService = Executors.newCachedThreadPool();

        for (int i = 0; i < threadTotal; i++) {
            final int count = i;

            executorService.execute(new Runnable() {
                @Override
                public void run() {

                    try {
                        test(count);
                    } catch (Exception e) {

                    } finally {
                        countDownLatch.countDown();
                    }
                }
            });
        }
        countDownLatch.await(100, TimeUnit.MILLISECONDS);
        log.info("finish");
        executorService.shutdown();
    }

    public static void test(int threadCount) throws InterruptedException {
        Thread.sleep(101);
        log.info("thread : {}", threadCount);
    }
}
